from typing import Optional, List, Union

from pydantic import BaseModel, Field


class OperateResp(BaseModel):
    state: bool
    error: Optional[str] = None


class PathInfo(BaseModel):
    cid: int
    pid: int
    name: str


class FolderResp(BaseModel):
    cid: int
    n: str
    te: int = None
    pid: int = None
    pc: Optional[str] = None
    is_root: bool = False


class FileResp(BaseModel):
    cid: int
    n: str
    te: int
    fid: int
    s: int
    sha: str
    pc: str  # pick_code
    play_long: Optional[int] = None


class DirList(OperateResp):
    data: List[Union[FileResp, FolderResp]] = list()
    count: int
    path: List[PathInfo] = list()
    order: str
    is_asc: int


class DownloadInfoEnc(OperateResp):
    """加密的下载信息"""
    data: str


class DownloadURL(BaseModel):
    url: str
    client: int
    oss_id: str


class DownloadInfo(BaseModel):
    file_name: str
    file_size: int
    pick_code: str
    url: Union[DownloadURL, bool]


class UploadInfo(OperateResp):
    user_id: int
    user_key: str = Field(None, alias="userkey")
    size_limit: int


class TokenInfo(BaseModel):
    endpoint: str
    token_url: str = Field(None, alias="gettokenurl")


class UploadToken(BaseModel):
    access_key_id: str = Field(None, alias="AccessKeyId")
    access_key_secret: str = Field(None, alias="AccessKeySecret")
    security_token: str = Field(None, alias="SecurityToken")
    expiration: str = Field(None, alias="Expiration")
    status_code: int = Field(None, alias="StatusCode")


class DigestResult(BaseModel):
    size: int = 0
    pre_id: str = None
    quick_id: str = None
    md5: str = None


FileOrFolder = Union[FileResp, FolderResp]